﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace APP
{
    public class HttpHelper
    {
        public static string HttpGet(string url,CookieContainer cookieContainer)
        {
            string result = string.Empty;
            cookieContainer = new CookieContainer();
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "GET";
                wbRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                wbRequest.KeepAlive = false;
                wbRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0";
                wbRequest.CookieContainer = cookieContainer;
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                cookieContainer.Add(wbResponse.Cookies);
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sReader = new StreamReader(responseStream))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return result;
        }
        #region 弃用
        //public static string PbSouGouHttpGetList(string url, CookieContainer cookieContainer)
        //{
        //    string result = string.Empty;
        //    try
        //    {
        //        HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
        //        wbRequest.Method = "GET";
        //        wbRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        //        wbRequest.KeepAlive = false;
        //        wbRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0";
        //        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
        //        long t = (DateTime.Now.Ticks - startTime.Ticks) / 10000;
        //        wbRequest.CookieContainer = cookieContainer;
        //        HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
        //        var responsecookie= wbResponse.Cookies;
        //        cookieContainer.Add(responsecookie);
        //        using (Stream responseStream = wbResponse.GetResponseStream())
        //        {
        //            using (StreamReader sReader = new StreamReader(responseStream))
        //            {
        //                result = sReader.ReadToEnd();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //    return result;
        //}


        //public static string HttpGetPage(string url, CookieContainer cookieContainer)
        //{
        //    string result = string.Empty;
        //    try
        //    {
        //        HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
        //        wbRequest.Method = "GET";
        //        wbRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        //        wbRequest.KeepAlive = false;
        //        wbRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0";
        //        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
        //        long t = (DateTime.Now.Ticks - startTime.Ticks) / 10000;
        //        wbRequest.CookieContainer = cookieContainer;
        //        HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
        //        cookieContainer.Add(wbResponse.Cookies);
        //        using (Stream responseStream = wbResponse.GetResponseStream())
        //        {
        //            using (StreamReader sReader = new StreamReader(responseStream))
        //            {
        //                result = sReader.ReadToEnd();
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //    return result;
        //} 
        #endregion

        public static string HttpPost(string url, string paramData, Dictionary<string, string> headerDic = null)
        {
            string result = string.Empty;
            try
            {
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "POST";
                wbRequest.ContentType = "application/x-www-form-urlencoded";
                wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
                if (headerDic != null && headerDic.Count > 0)
                {
                    foreach (var item in headerDic)
                    {
                        wbRequest.Headers.Add(item.Key, item.Value);
                    }
                }
                using (Stream requestStream = wbRequest.GetRequestStream())
                {
                    using (StreamWriter swrite = new StreamWriter(requestStream))
                    {
                        swrite.Write(paramData);
                    }
                }
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    using (StreamReader sread = new StreamReader(responseStream))
                    {
                        result = sread.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            { }

            return result;
        }
    }
}
